
import { _decorator } from 'cc';
const { ccclass, property } = _decorator;
import * as cc from 'cc';
import { VMCenter } from '../framework/vm/VMCenter';
import { VMBaseAttr, VMEntity } from '../framework/vm/VMEntity';

/**
 * Predefined variables
 * Name = TestScene
 * DateTime = Sun Mar 13 2022 10:02:28 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = TestScene.ts
 * FileBasenameNoExtension = TestScene
 * URL = db://assets/scripts/scene/TestScene.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 


let globalData = {
    touchCount: 0,
}

@ccclass('TestScene')
export class TestScene extends cc.Component implements IMVVM{
    _vmTag: string = "TestScene";
    _vmEntitys: VMEntity[];


    @property(cc.Sprite)
    sprite: cc.Sprite;

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Label)
    touchLabel: cc.Label = null;

    data = {
        count: 1,
        logo: "icon_floor1/spriteFrame",
        values: [3,2,3,4]
    }

    start () {

    }
    
    onLoad(){
        this.touchLabel.node.on(cc.Node.EventType.TOUCH_START,()=>{
            globalData.touchCount ++;
        });
        console.log(`TestScene-> `);
        
        this.schedule(()=>{this.data.count ++},1);
        this.scheduleOnce(()=>{
            // this.data.logo = "icon_floor1/spriteFrame";

            cc.tween(this.sprite.node).to(0.3,{position: cc.v3(100)}).start()
        },3);
    }

    onEnable(){
        VMCenter.add(this.data,this);
        VMCenter.add(globalData,"globalData");

        VMCenter.Sprite(this,this.sprite,{watchPath :"*.logo"});
        
        VMCenter.Label(this,this.label,{watchPath :"*.count"});

        VMCenter.Label(this,this.touchLabel,{watchPath :"globalData.touchCount"});

        for (let i = 0; i < this.data.values.length; i++) {
            let node = cc.find(`Canvas/Label${i+1}`);

            
            VMCenter.Label(this,node.getComponent(cc.Label),{watchPath :`*.values.${i}`,formator(opts){
                return opts.newValue;
            }});
        }
    }
    onDisable(){
        VMCenter.remove(this);
        VMCenter.remove(this,"globalData");
    }

    onClick(){
        cc.director.loadScene("scene-2d-2");
    }
    
}
